package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListFragmentTest extends Activity {
	static String[] WORD;
	static String[] DESC;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		WORD = getResources().getStringArray(R.array.word);
		DESC = getResources().getStringArray(R.array.desc);

		FragmentManager fm = getFragmentManager();
		if (fm.findFragmentById(android.R.id.content) == null) {
			WordListFragment wl = new WordListFragment();
			fm.beginTransaction().add(android.R.id.content, wl).commit();
		}
	}

	public static class WordListFragment extends ListFragment {
		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(
					getActivity(), android.R.layout.simple_list_item_1, WORD);
			setListAdapter(adapter);
		}

		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			super.onListItemClick(l, v, position, id);
			String text = WORD[position] + " : " + DESC[position];
			Toast.makeText(getActivity(), text, 0).show();
		}
	}
}
